package cn.goodym.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.goodym.exception.CustomException;
import cn.goodym.po.Userlogin;
import cn.goodym.service.UserloginService;

@Controller
@RequestMapping("/admin")
public class AdminController {

	@Resource(name = "userloginServiceImpl")
	private UserloginService userloginService;

	/**
	 * 普通用户账号密码重置跳转页面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/userPasswordRest")
	public String userPasswordRestUI() throws Exception {
		return "admin/userPasswordRest";
	}

	/**
	 *  普通用户账号密码重置处理
	 * @param userlogin
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/userPasswordRest", method = { RequestMethod.POST })
	public String userPasswordRest(Userlogin userlogin) throws Exception {

		Userlogin u = userloginService.findByName(userlogin.getUsername());

		if (u != null) {
			if (u.getRole() == 0) {//0为管理员
				throw new CustomException("该账户为管理员账户，没法修改");
			}
			u.setPassword(userlogin.getPassword());
			userloginService.updateByName(userlogin.getUsername(), u);
		} else {
			throw new CustomException("没找到该用户");
		}

		return "admin/userPasswordRest";
	}

	/**
	 * 修改本地账户
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/editPassword")
	public String editPasswordUI() throws Exception {
		return "admin/editpassword";
	}

}
